package comgame.scenes;

import com.almasb.fxgl.app.scene.FXGLScene;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.audio.Music;
import javafx.animation.Interpolator;
import javafx.animation.TranslateTransition;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.control.Label;
import javafx.util.Duration;
import javafx.scene.layout.StackPane;

public class PlanteraBossScene extends FXGLScene {
    private ImageView background;
    private ImageView bossView;
    private Label hpLabel;
    private int bossPhase = 1;
    private int bossHealth = 2000;
    private int bossMaxHealth = 2000;
    private Music bossMusic;

    public PlanteraBossScene() {
        // 设置背景
        background = new ImageView(new Image(getClass().getResourceAsStream("/assets/textures/BossBackGround.png")));
        background.setFitWidth(FXGL.getAppWidth());
        background.setFitHeight(FXGL.getAppHeight());
        getContentRoot().getChildren().add(background);

        // Boss第一阶段
        bossView = new ImageView(new Image(getClass().getResourceAsStream("/assets/textures/Monster_Plantera.gif")));
        bossView.setFitWidth(500);
        bossView.setFitHeight(500);
        bossView.setLayoutY(100);
        bossView.setLayoutX(FXGL.getAppWidth() + 100); // 初始在屏幕外右侧
        getContentRoot().getChildren().add(bossView);

        // Boss血条
        hpLabel = new Label("BOSS HP: " + bossHealth + "/" + bossMaxHealth);
        hpLabel.setStyle("-fx-font-size: 32px; -fx-text-fill: #ff69b4; -fx-font-weight: bold; -fx-effect: dropshadow(one-pass-box, #000, 2, 0, 1, 1);");
        hpLabel.setLayoutX(50);
        hpLabel.setLayoutY(30);
        getContentRoot().getChildren().add(hpLabel);

        // 平移动画：缓缓进入屏幕中央
        double targetX = (FXGL.getAppWidth() - bossView.getFitWidth()) / 2;
        TranslateTransition moveIn = new TranslateTransition(Duration.seconds(2.5), bossView);
        moveIn.setFromX(0);
        moveIn.setToX(targetX - bossView.getLayoutX());
        moveIn.setInterpolator(Interpolator.EASE_BOTH);
        moveIn.setOnFinished(e -> {
            // Boss到达中央后可开启攻击、技能等
        });
        moveIn.play();

        // 播放Boss音乐
        bossMusic = FXGL.getAssetLoader().loadMusic("BossFight.mp4");
        FXGL.getAudioPlayer().loopMusic(bossMusic);
    }



    // 示例：受到伤害
    public void damageBoss(int dmg) {
        bossHealth -= dmg;
        if (bossHealth < bossMaxHealth / 2 && bossPhase == 1) {
            switchToSecondPhase();
        }
        if (bossHealth <= 0) {
            bossHealth = 0;
            win();
        }
        hpLabel.setText("BOSS HP: " + bossHealth + "/" + bossMaxHealth);
    }

    private void switchToSecondPhase() {
        bossPhase = 2;
        bossView.setImage(new Image(getClass().getResourceAsStream("/assets/textures/Plantera_(Second_form).gif")));
        // 可加特效、对话等
    }

    private void win() {
        FXGL.getAudioPlayer().stopMusic(bossMusic);
        // 可弹出胜利UI、奖励、剧情等
        hpLabel.setText("BOSS已被击败！");
    }
} 